//
//  ST_URL+FW.swift
//  BTVideo
//
//  Created by DouLei on 2019/8/12.
//  Copyright © 2019 DouLei. All rights reserved.
//

import Foundation
import UIKit
import AVFoundation
import DeviceKit




extension URL{
    
    
    func get_video_thumbnail_image(time:CMTime = CMTimeMake(value: 1, timescale: 60)) -> UIImage? {
        
        let asset: AVAsset = AVAsset(url: self)
        
        let imageGenerator = AVAssetImageGenerator(asset: asset)
        
        imageGenerator.appliesPreferredTrackTransform = true
        
//        let currentTime: CMTime = player.currentTime() // step 1.
        let currentTimeInSecs: Float64 = CMTimeGetSeconds(time) // step 2.
        
        let actionTime: CMTime = CMTimeMake(value: Int64(currentTimeInSecs), timescale: 1)
        
//        let newValue = Float(time.value) * Float(time.timescale)/60.0
        
//        let currentTime = CMTimeMake(value: Int64(newValue), timescale: 60)
        
        do {
            let thumbnailImage = try imageGenerator.copyCGImage(at: actionTime, actualTime: nil)
            return UIImage(cgImage: thumbnailImage)
        } catch let error {
            print(error)
        }
        return nil
    }
    
}


extension String{
    
    /**
     大图图片
     */
    func big_remote_image() -> String {
        
        return self

    }
    /**
     压缩图片
     */
    func compress_image() -> String {

        let smallImgDevices:[Device] = [.iPhone6, .iPhone6Plus, .iPhone6s, .iPhone6sPlus]
        
        let midImgDevices: [Device] = [.iPhone7,.iPhone7Plus]

        let device = Device.current
        
        var configStr = SCComponets.asset_config.big_image_suffix
         
        if smallImgDevices.contains(device) {
            configStr = SCComponets.asset_config.small_image_suffix
        }else if midImgDevices.contains(device){
            configStr = SCComponets.asset_config.mid_image_suffix
        }
        
        return handle_config(configStr: configStr)
    }
    
    ///处理是否压缩图片
    fileprivate func handle_config(configStr:String) -> String {
        
        //不包括 jingniao 不处理 返回原图
        if self.contains("jingniao") == false{
            return self
        }
        
        
        guard var urlComponents = URLComponents(string: self)  else { return self }
        
        var queryItems = urlComponents.queryItems ?? [URLQueryItem]()
        
    //    后台自己没有添加缩略图 前端处理
        if queryItems.contains(where: { $0.name.contains("imageView2") }) == false{
            
            let item = URLQueryItem(name: configStr, value: nil)
            
            queryItems.insert(item, at: 0)
            
            urlComponents.queryItems = queryItems
            
            return  urlComponents.string ?? self
        }
        
        return self
    }

    
    
    
    /**
     广告页图片大小
     */
    func advert_image_url_str() -> String {
       
        let smallImgDevices:[Device] = [.iPhone6, .iPhone6Plus, .iPhone6s, .iPhone6sPlus]
        
        let midImgDevices: [Device] = [.iPhone7,.iPhone7Plus]

        let device = Device.current
        
        var configStr:String?
         
        if smallImgDevices.contains(device) {
            configStr = "imageView2/0/w/375/h/375/interlace/1"
        }else if midImgDevices.contains(device){
            configStr = "imageView2/0/w/750/h/750/interlace/1"
        }
        
        // 需要压缩
        if let c = configStr{
            return handle_config(configStr: c)
        }else{
            return self
        }
    }
    
    
    /**
     压缩小图片
     */
    func compress_small_image(width:CGFloat = 100) -> String {
        
        let configStr = "imageView2/0/w/\(width.int)/h/\(width.int)/interlace/1"
        
        return handle_config(configStr: configStr)
    }
    
}
